namespace Adge.Common
{
    using System;
    using System.Runtime.CompilerServices;
    using System.Web;
    using System.Web.Caching;

    public class DefaultCacheStrategy : ICacheStrategy
    {
        static DefaultCacheStrategy()
        {
            DefaultCacheStrategy.instance = new DefaultCacheStrategy();
            DefaultCacheStrategy.webCache = null;
            DefaultCacheStrategy.syncObj = new object();
            lock (DefaultCacheStrategy.syncObj)
            {
                HttpContext context1 = HttpContext.Current;
                if (context1 != null)
                {
                    DefaultCacheStrategy.webCache = (Cache)context1.Cache;
                }
                else
                {
                    DefaultCacheStrategy.webCache = (Cache)HttpRuntime.Cache;
                }
            }
        }

        public DefaultCacheStrategy()
        {
            this._timeOut = 0x1770;
        }

        public void Accept(LogVisitor lv)
        {
            this.myLogVisitor = lv;
        }

        public void AddObject(string objId, object o)
        {
            if (((objId != null) && (objId.Length != 0)) && (o != null))
            {
                CacheItemRemovedCallback callback1 = new CacheItemRemovedCallback(this.onRemove);
                if (this.TimeOut == 60)
                {
                    DefaultCacheStrategy.webCache.Insert(objId, o, null, DateTime.MaxValue, TimeSpan.Zero, CacheItemPriority.High, callback1);
                }
                else
                {
                    DefaultCacheStrategy.webCache.Insert(objId, o, null, DateTime.Now.AddMinutes((double)this.TimeOut), TimeSpan.Zero, CacheItemPriority.High, callback1);
                }
            }
        }

        public void AddObjectWith(string objId, object o)
        {
            if (((objId != null) && (objId.Length != 0)) && (o != null))
            {
                CacheItemRemovedCallback callback1 = new CacheItemRemovedCallback(this.onRemove);
                DefaultCacheStrategy.webCache.Insert(objId, o, null, DateTime.Now.AddHours((double)this.TimeOut), Cache.NoSlidingExpiration, CacheItemPriority.High, callback1);
            }
        }

        public void AddObjectWithDepend(string objId, object o, string[] dependKey)
        {
            if (((objId != null) && (objId.Length != 0)) && (o != null))
            {
                CacheItemRemovedCallback callback1 = new CacheItemRemovedCallback(this.onRemove);
                CacheDependency dependency1 = new CacheDependency(null, dependKey, DateTime.Now);
                DefaultCacheStrategy.webCache.Insert(objId, o, dependency1, DateTime.Now.AddMinutes((double)this.TimeOut), Cache.NoSlidingExpiration, CacheItemPriority.High, callback1);
            }
        }

        public void AddObjectWithFileChange(string objId, object o, string[] files)
        {
            if (((objId != null) && (objId.Length != 0)) && (o != null))
            {
                CacheItemRemovedCallback callback1 = new CacheItemRemovedCallback(this.onRemove);
                CacheDependency dependency1 = new CacheDependency(files, DateTime.Now);
                DefaultCacheStrategy.webCache.Insert(objId, o, dependency1, DateTime.Now.AddHours((double)this.TimeOut), Cache.NoSlidingExpiration, CacheItemPriority.High, callback1);
            }
        }

        public void onRemove(string key, object val, CacheItemRemovedReason reason)
        {
            switch (reason)
            {
                case CacheItemRemovedReason.Removed:
                    {
                        CacheItemRemovedCallback callback2 = new CacheItemRemovedCallback(this.onRemove);
                        DefaultCacheStrategy.webCache.Insert(key, val, null, DateTime.Now.AddMinutes((double)this.TimeOut), Cache.NoSlidingExpiration, CacheItemPriority.High, callback2);
                        break;
                    }
                case CacheItemRemovedReason.Expired:
                    {
                        CacheItemRemovedCallback callback1 = new CacheItemRemovedCallback(this.onRemove);
                        DefaultCacheStrategy.webCache.Insert(key, val, null, DateTime.Now.AddMinutes((double)this.TimeOut), Cache.NoSlidingExpiration, CacheItemPriority.High, callback1);
                        break;
                    }
                case CacheItemRemovedReason.Underused:
                    {
                        CacheItemRemovedCallback callback3 = new CacheItemRemovedCallback(this.onRemove);
                        DefaultCacheStrategy.webCache.Insert(key, val, null, DateTime.Now.AddMinutes((double)this.TimeOut), Cache.NoSlidingExpiration, CacheItemPriority.High, callback3);
                        break;
                    }
            }
            this.myLogVisitor.WriteLog(this, key, val, reason);
        }

        public void RemoveObject(string objId)
        {
            if ((objId != null) && (objId.Length != 0))
            {
                DefaultCacheStrategy.webCache.Remove(objId);
            }
        }

        public object RetrieveObject(string objId)
        {
            if ((objId == null) || (objId.Length == 0))
            {
                return null;
            }
            return DefaultCacheStrategy.webCache.Get(objId);
        }


        public int TimeOut
        {
            get
            {
                return ((this._timeOut > 0) ? this._timeOut : 0x1770);
            }
            set
            {
                this._timeOut = (value > 0) ? value : 0x1770;
            }
        }


        private int _timeOut;
        private static readonly DefaultCacheStrategy instance;
        private LogVisitor myLogVisitor;
        private static object syncObj;
        private static volatile Cache webCache;
    }
}

